package com.szsh.aiot.hsm.controller;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.szsh.aiot.common.annotation.CustomPermission;
import com.szsh.aiot.common.annotation.Log;
import com.szsh.aiot.common.core.domain.AjaxResult;
import com.szsh.aiot.common.core.domain.RequestDto;
import com.szsh.aiot.common.enums.BusinessType;
import com.szsh.aiot.hsm.dto.enu.NumberEnum;
import com.szsh.aiot.hsm.service.CameraAccessService;
import com.szsh.aiot.hsm.utils.PermitValidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.List;

/**
 * @Description:摄像头访问提醒功能
 * @Date 2023/8/25: 14:06
 * @Author: LOADING>>>>>
 */

@Controller
@RequestMapping("/app/appSafe")
public class CameraAccessController {

    private static final Logger LOGGER = LoggerFactory.getLogger(CameraAccessController.class);

    @Autowired
    private PermitValidUtil permitValidUtil;

    @Autowired
    private CameraAccessService cameraAccessService;

    @Autowired
    private Gson gson;

    /**
     * 4.1 通过此接口可以对摄像头访问提醒方式增删改查操作;
     *
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @RequestMapping(value = "/camera/data")
    @Log(title = "摄像头提醒接口", businessType = BusinessType.OTHER)
    @CustomPermission
    @ResponseBody
    public AjaxResult cameraAccessReminder(@RequestBody RequestDto requestDto) {

        String sequenceNo = requestDto.getSequenceNo();
        LOGGER.info("请求参数为{},sequenceNo={}",requestDto.getRequestJsonElement(),sequenceNo);
        JsonObject requestParams = requestDto.getRequestJsonElement().getAsJsonObject();
        String operaType = permitValidUtil.getElementValue(requestParams, "operaType");
        String broadbandNO = permitValidUtil.getElementValue(requestParams, "broadbandNO");

        LOGGER.info("请求参数为{},operaType={}",requestDto.getRequestJsonElement(),operaType);
        switch (operaType) {
            case NumberEnum.STR_1:
                String gatewayMac = permitValidUtil.getElementValue(requestParams, "gatewayMac");
                String pushReason = permitValidUtil.getElementValue(requestParams, "pushReason");
                return cameraAccessService.getCameraAbnormal(broadbandNO,gatewayMac,pushReason);
            //摄像头访问首页，摄像头列表及异常访问信息
            case NumberEnum.STR_2:
                return cameraAccessService.queryRemindSetting(broadbandNO);
            //查询用户设置的命中方式
            case NumberEnum.STR_3:
                Integer setting = Integer.valueOf(permitValidUtil.getElementValue(requestParams, "settingInfo"));
                return cameraAccessService.updateRemindSetting(broadbandNO,setting);
            //更新摄像头访问提醒设置
            case NumberEnum.STR_4:
                gatewayMac = permitValidUtil.getElementValue(requestParams, "gatewayMac");
                return cameraAccessService.getDeviceList(broadbandNO,gatewayMac);
            //获取用户待选设备列表
            case NumberEnum.STR_5:
                List<String> deviceIdList = gson.fromJson(requestParams.get("settingInfo"),new TypeToken<List<String>>(){}.getType());
                return cameraAccessService.saveDeviceInfo(deviceIdList,broadbandNO);
            //保存用户自定义摄像头异常访问设备列表信息
            default:
                AjaxResult.badRequest("未知操作，请确认后重新操作");
                break;
        }
        return AjaxResult.badRequest("未知操作，请确认后重新操作");
    }
}
